﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using HuffmanClasses;

namespace ECoding
{
    public partial class MainForm : Form
    {
        private IArchiveReader _archiveReader;
        private IDecompressor _decompressor = new HuffmanDecompressor();
        public MainForm()
        {
            InitializeComponent();
            PasswordTextBox.Enabled = SetPasswordCheckBox.Checked;
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            String filePath;
            AddfileDialog.FileName = "";
            if (AddfileDialog.ShowDialog() == DialogResult.Cancel)
                return;
            filePath = AddfileDialog.FileName;
            if (FilesListBox.Items[0] == " ") FilesListBox.Items[0] = filePath;
            else FilesListBox.Items.Add((object)filePath);
            FilesListBox.SetSelected(FilesListBox.Items.Count - 1, true);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            PasswordTextBox.Enabled = SetPasswordCheckBox.Checked;
            PasswordTextBox.Text = "";
        }

        private void MakeButton_Click(object sender, EventArgs e)
        {
            
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                List<String> sourceFiles = new List<string>();
                foreach (var item in FilesListBox.Items)
                {
                    sourceFiles.Add((string)item);
                }
                ICompressor compressor = new HuffmanCompressor();
                IArchiveWriter archiveWriter = new HuffmanSolidArchiveWriter();
                archiveWriter.PackFiles(sourceFiles, saveFileDialog.FileName, compressor);    
            }
            

        }

        private void OpenArhiveButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                _archiveReader = new HuffmanSolidArchiveReader(openFileDialog.FileName);
                List<KeyValuePair<string, long>> fileInfos = _archiveReader.GetFileInfos();
                foreach (var fileInfo in fileInfos)
                {
                    ArhiveContentListBox.Items.Add(fileInfo.Key);
                }
            }
        }

        private void UnpackButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                _archiveReader.UnPackToFolder(folderBrowserDialog.SelectedPath, _decompressor);
            }
        }
    }

    
}